using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Umdf.SideShow.Scf.ObjectModel;
using Umdf.SideShow.PocketPC;
using Umdf.SideShow;
using Umdf.SideShow.Contents;

namespace SideShowClient
{
    public partial class ScfMenuControl : ScfUserControl
    {
        private bool _initializing;

        public ScfMenuControl(ScfNavigator navigator) : base(navigator)
        {
            InitializeComponent();
        }

        protected override void BindData()
        {
            _initializing = true;
            lvMenuItems.Items.Clear();
            imlSmallImages.Images.Clear();
            ListViewItem def = null;
            ScfMenu scfMenu = (ScfMenu)this.ScfObject;
            foreach (ScfObject obj in scfMenu.Children)
            {
                if (obj is ScfMenuItem)
                {
                    ScfMenuItem item = (ScfMenuItem)obj;
                    if (item.ImageId != 0)
                    {
                        Image img = Navigator.GetImage(item.ImageId);
                        imlSmallImages.Images.Add(item.ImageId.ToString(), img);
                    }
                    if (item.Enabled)
                    {
                        ListViewItem lvi = new ListViewItem(item.Text, item.ImageId.ToString());
                        lvi.Tag = item;
                        lvMenuItems.Items.Add(lvi);
                        if (item.IsDefault)
                        {
                            def = lvi;
                        }
                    }
                }
            }
            if (def == null && lvMenuItems.Items.Count > 0)
            {
                def = lvMenuItems.Items[0];
            }
            //def.Selected = true;
            _initializing = false;
        }

        private void lvMenuItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!_initializing && lvMenuItems.SelectedItems.Count > 0)
            {
                ScfMenuItem item = (ScfMenuItem)lvMenuItems.SelectedItems[0].Tag;
                Navigator.Navigate(item);
            }
        }

        private void ScfMenuControl_Resize(object sender, EventArgs e)
        {
            columnHeader1.Width = lvMenuItems.ClientSize.Width;
        }
    }
}
